#ifdef _PLATFORM_WIN32
#include "registry.h"

using namespace util;

Registry * Registry::m_reg = 0;

Registry::Registry ( )
{
	m_hKey		= 0;
	m_subKey	= NULL;

    m_class		= 0;
	m_options	= 0;
	m_sam		= KEY_ALL_ACCESS;
	m_secAttrib	= 0;
}

Registry::~Registry ( )
{
	delete m_subKey;
}

Registry * Registry::getInstance ( )
{
    if ( !m_reg )
    {
        m_reg = new Registry ( );
    }
    return m_reg;
}

DWORD Registry::create ( )
{
	LONG errcode = RegCreateKeyEx ( m_hKey, m_subKey, 0, m_class, m_options, m_sam, m_secAttrib, &m_hRegKey, &m_result );

	if (errcode != ERROR_SUCCESS)
	{
		return 0;
	}
	return m_result;
}

bool Registry::setVal ( const char* valname, DWORD valtype, void* valdata, DWORD valsize )
{
	LONG errcode = RegSetValueEx ( m_hRegKey, valname, 0, valtype, ( LPBYTE ) valdata, valsize );

	if ( errcode != ERROR_SUCCESS )
	{
        return false;
	}
    else
    {
        return true;
    }
}

bool Registry::queryVal ( const char* valname, DWORD * valtype, void * valdata, DWORD* valsize )
{
    DWORD size;
    DWORD type;

    LONG errcode = RegQueryValueEx ( m_hRegKey, valname, 0, &type, (LPBYTE) valdata, &size );

	if ( errcode != ERROR_SUCCESS )
	{
		return false;
	}
	else
	{
		return true;
	}
}

bool Registry::deleteVal ( const char* valname )
{
	LONG errcode = RegDeleteValue ( m_hRegKey, valname );

	if ( errcode != ERROR_SUCCESS )
	{
        return false;
	}
    else
    {
        return true;
    }
}

bool Registry::isValExist ( const char* valname )
{
	DWORD type = 0;
	DWORD size = 1024;
	char  str[1024];

	str[0] = 0;

	return queryVal ( valname, &type, (void*) str, &size ); 
}

void Registry::setParams ( HKEY hkey, char* subkey )
{
	m_hKey		= hkey;
	m_subKey	= new char[strlen(subkey)+1];
	strcpy ( m_subKey, subkey );
}

/*
        LPVOID lpMsgBuf;
        if (!FormatMessage( 
            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM | 
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            errcode,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
            (LPTSTR) &lpMsgBuf,
            0,
            NULL ))
        {
        // Handle the error.
        return false;
        }

        // Process any inserts in lpMsgBuf.
        // ...

        // Display the string.
        MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );

        // Free the buffer.
        LocalFree( lpMsgBuf );
*/
#endif
